﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace ClickrDesktop.Helpers
{
    public class CSVParser
    {
        public static DataTable csvToDataTable(String filePath, bool isRowOneHeader)
        {
            DataTable csvDataTable = new DataTable();
            try
            {
                String[] csvData = File.ReadAllLines(filePath);
                if (csvData.Length == 0) { throw new Exception("CSV File Appears to be Empty"); }

                String[] headings = csvData[0].Replace("\"", "").Split(',');
                int index = isRowOneHeader ? 1 : 0;

                if (isRowOneHeader)
                {
                    for (int i = 0; i <= headings.Length - 1; i++)
                    {
                        headings[i] = headings[i].Replace(" ", " ").Replace("\"", "");
                        csvDataTable.Columns.Add(headings[i], typeof(string));
                    }
                }
                else
                    for (int i = 0; i <= headings.Length - 1; i++)
                        csvDataTable.Columns.Add("col" + (i + 1).ToString(), typeof(string));

                for (int i = index; i <= csvData.Length - 1; i++)
                {
                    DataRow row = csvDataTable.NewRow();
                    for (int j = 0; j <= headings.Length - 1; j++)
                        try { row[j] = csvData[i].Replace("\"", "").Split(',')[j].Replace("\"", ""); } catch (Exception) { };

                    csvDataTable.Rows.Add(row);
                }
            }
            catch (Exception ex) { }

            return csvDataTable;
        }

        public static Boolean DataTableToCsv(string filePath, DataTable dt, bool writeColumnNames, bool AppendText)
        {
            if (dt == null) { return false; }
            if (dt.Columns.Count <= 0) { return false; }

            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(filePath, AppendText);

                if (writeColumnNames)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        sw.Write(dt.Columns[i].ColumnName);
                        if (i < dt.Columns.Count - 1)
                        {
                            sw.Write(",");
                        }
                    }

                    sw.Write(sw.NewLine);
                }

                foreach (DataRow item in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (!String.IsNullOrEmpty(item[i].ToString()) && item[i] != DBNull.Value)
                            sw.Write(item[i].ToString());
                        else
                            sw.Write(String.Empty);

                        if (i < dt.Columns.Count - 1)
                            sw.Write(",");
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
            }
            catch (Exception ex)
            {
                sw.Close();
                return false;
            } 
            return true;
        }

        public static DataRow CheckIfExists (DataTable dtTable, String ColumnName, String ColumnValue)
        {
            DataRow[] rows = dtTable.Select(String.Format("{0} = '{1}'", ColumnName, ColumnValue));
            if (rows.Length > 0)
                return rows.First();

            return null;
        }

        public static int getRowNumber(DataTable dtTable, DataRow row)
        {
            return dtTable.Rows.IndexOf(row);
        }
    }
}
